package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 权限服务
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    //新增权限
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);

    }

    @Override
    public PageResult findPage(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除权限
    @Override
    public void deleteById(Integer id) throws RuntimeException {
        //查询当前权限是否和 角色 关联
        long count = permissionDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前权限已被角色引用，不能删除");
        }
        permissionDao.deleteById(id);

    }

    // 主键查询
    @Override
    public Permission findById(Integer id) {
        Permission permission = permissionDao.findPermissionById(id);
        return permission;
    }

    // 编辑保存
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);

    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }



}
